#include "main.h"

INT32U Reload;

#ifdef OS_CRITICAL_METHOD
static INT16U ms_Reload = 10500; //1ms的时间基准 25000000 / 1000 / 8 = 10500，形成10us的基准需要105，但是无法形成1us的时间基准
static INT16U us_Reload = 105; //注意这是10us的时间基准

static INT8U fac_ms = 1000 / OS_TICKS_PER_SEC;

void Delay_us(INT32U nus){
    INT32U ticks_all = nus*us_Reload;
    INT32U count = 0 ;
    OSSchedLock();
    INT32U now;
    INT32U previous = SysTick->VAL;
    while(count < ticks_all){
        now = SysTick->VAL;
        if(now >= previous) {
            count += ms_Reload + previous - now;
        }
        else {
            count += now - previous;
        }
        previous = now;
    }
    OSSchedUnlock();
}

void Delay_ms(INT32U nms){
        // If OS is running
    if(OSRunning == OS_TRUE && OSLockNesting == 0)
    {
        // If delay time is greater than the operating system minimum unit
        if(nms >= fac_ms)
        {
            // OS time delay
            OSTimeDly(nms / fac_ms);
        }
        // Use normal method to delay
        nms %= fac_ms;
    }
    // Normal method to delay
    Delay_us((INT32U)(nms * 1000));
}



#else




#endif
